package com.vgemv.jsutilitysdkdemo.module.Menu.FilmList;


import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.core.content.ContextCompat;

import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdkdemo.R;


public class FilmCell extends JsMenuItemView {


    public TextView mtv;
    public ImageView miv;
    public ImageView miv_delete;
    public ImageView miv_upload;
    public ConstraintLayout cl_container;
    public View mv_border;
    public TextView mtv_checkTime;
    public TextView mtv_checkReason;
    public View mv_dash;
    public final static int ActionDelete = 0;
    public final static int ActionSelect = 1;
    public final static int ActionUpload = 2;
    public ConstraintSet mcs;

    public FilmCellClickCallback mClickCallback;

    //图片高300,文字和按钮

    FilmCell(Context context) {
        super(context);
    }

    @Override
    public void customSetVm(Context context, JsMenuItemVm itemVm) {
        FilmVm filmVm = (FilmVm)itemVm;
        FilmModel model = (FilmModel)filmVm.currentModel();
        setupUi(filmVm.index,model);
    }

    //可能会重复调用
    public void setupUi(final int position, final FilmModel model) {
        try {
            if(mcs == null) {
                mcs = new ConstraintSet();
            }

            int imageSize = 60;
            //需要设置边框和内容的间距,否则内容会把边框给遮盖了..
            int borderContentPadding = 10;
            {
                if(mv_border == null) {
                    mv_border = new View(this.getContext());
                    mv_border.setBackgroundResource(R.drawable.corners_border);
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    mv_border.setLayoutParams(lp);
                    addView(mv_border);
                }
            }

            {
                //总体的线性布局
                //CardView可以的subview可以通过设置LinearLayout.LayoutParams来布局.
                if (cl_container == null) {

                    cl_container = new ConstraintLayout(this.getContext());
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    lp.setMargins(borderContentPadding,borderContentPadding,borderContentPadding,borderContentPadding);
                    cl_container.setLayoutParams(lp);
                    addView(cl_container);
                }
            }

            {
                //image,代码产生的view没有id,必须手动设置.
                if (miv == null) {
                    miv = new ImageView(this.getContext());
                    miv.setId(View.generateViewId());
                    //lp也是有效的
                    ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500);
                    lp.leftMargin = 0;
                    lp.rightMargin = 0;
                    miv.setLayoutParams(lp);
                    miv.setBackgroundColor(Color.parseColor("#000000"));
                    cl_container.addView(miv);
                }


                //加载图片
                String filmPath = model.moviePath;
                String imagePath = model.imagePath;

//                if(model.isRemote) {
//                    Picasso.get()  //之前的版本是用Picasso.with(context)
//                            .load(imagePath)//load 方法加载图片:可以是网络图片url或者file 路径或者content资源或者Android Resource。
//                            .centerCrop()//设置scaleType为centerCrop，充满ImageView 的边界，居中裁剪
////                            .placeholder(R.mipmap.film)//网络请求还没有完成时显示的本地图片
//                            .error(R.mipmap.ic_launcher)//网络请求错误时显示的本地图片
////                        .resize(400,300)//设置图片尺寸
////                        .onlyScaleDown()//当调用了resize 方法设置图片尺寸时，调用onlyScaleDown 方法，只有当原始图片的尺寸大于我们指定的尺寸时，resize才起作用
//                            .fit()//fit 它会自动测量我们的View的大小，然后内部调用reszie方法把图片裁剪到View的大小(fit 还是会出现拉伸扭曲的情况，因此最好配合前面的centerCrop使用)
//                            .tag("tag")//请求设置tag
////                        .rotate(60)//设置图片旋转角度（图片在显示前，有个旋转的动画效果）
////                        .memoryPolicy(0,0) //设置内存缓存策略为不缓存
////                        .networkPolicy(0,0)//设置磁盘缓存策略为不缓存
//                            .into(miv);//显示到指定的控件上
//                }
//                else {
//                                    InputStream is = new FileInputStream(imagePath);
//                    BitmapFactory.Options options = new BitmapFactory.Options();
//                    options.inSampleSize =3;
//                    Bitmap bitmap = BitmapFactory.decodeStream(is,null,options);
//                    miv.setImageBitmap(bitmap);
//                }
            }


            {
                //删除按钮
                if (miv_delete == null) {
                    miv_delete = new ImageView(this.getContext());
                    miv_delete.setId(View.generateViewId());
                    cl_container.addView(miv_delete);
                    if(model.isRemote) {
                        mcs.constrainHeight(miv_delete.getId(),imageSize);
                        mcs.constrainWidth(miv_delete.getId(),imageSize);
                        //底部和父视图对齐
                        mcs.connect(miv_delete.getId(), ConstraintSet.BOTTOM, miv.getId(), ConstraintSet.BOTTOM,20);
                        //右部和父视图对齐
                        mcs.connect(miv_delete.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT);

                        miv_delete.setBackground(ContextCompat.getDrawable(getContext(),R.mipmap.delete));
                        miv_delete.setScaleType(ImageView.ScaleType.FIT_CENTER);
                        miv_delete.setVisibility(View.VISIBLE);
                    }
                    else {
                        miv_delete.setVisibility(View.GONE);
                    }
                }
            }

            {
                //上传按钮
                if (model.isRemote) {
                    if (miv_upload != null) {
                        miv_upload.setVisibility(View.GONE);
                    }
                } else {
                    if (miv_upload == null) {
                        miv_upload = new ImageView(this.getContext());
                        miv_upload.setId(View.generateViewId());
                        cl_container.addView(miv_upload);
                        //宽高
                        mcs.constrainHeight(miv_upload.getId(),imageSize);
                        mcs.constrainWidth(miv_upload.getId(),imageSize);
                        //底部和父视图对齐
                        mcs.connect(miv_upload.getId(), ConstraintSet.BOTTOM, miv.getId(), ConstraintSet.BOTTOM,20);
                        //右部和和miv的左边对齐.
                        mcs.connect(miv_upload.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT,20);
//                        miv_upload.setLayoutParams(lp);
//                        miv_upload.setBackgroundColor(Color.argb(255, 100, 100, 100));

                        miv_upload.setBackground(ContextCompat.getDrawable(getContext(),R.mipmap.upload));
                        miv_upload.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    }

                }
            }
            {
                //TextView
                if (mtv == null) {
                    mtv = new TextView(this.getContext());
                    mtv.setId(View.generateViewId());
                    cl_container.addView(mtv);
                    mcs.constrainHeight(mtv.getId(),150);
                    mcs.connect(mtv.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT,20);
                    //底部和父视图对齐
                    mcs.connect(mtv.getId(), ConstraintSet.BOTTOM, miv.getId(), ConstraintSet.BOTTOM,20);



                    mtv.setTextColor(Color.parseColor("#ffffffff"));
//                    mtv.setBackgroundColor(Color.parseColor("#ffff0000"));
                    mtv.setGravity(Gravity.BOTTOM);
                }
                if (model.isRemote) {
                    //和删除按钮对齐
                    mcs.connect(mtv.getId(), ConstraintSet.RIGHT, miv_delete.getId(), ConstraintSet.LEFT);
                } else {
                    //和上传按钮对齐
                    mcs.connect(mtv.getId(), ConstraintSet.RIGHT, miv_upload.getId(), ConstraintSet.LEFT,20);
                }
                mtv.setText(model.title);

            }

            if(model.isRemote&&!model.isPassed) {
                {
                    if(mv_dash == null) {
                        mv_dash =  new View(this.getContext());
                        mv_dash.setId(View.generateViewId());
                        cl_container.addView(mv_dash);
                        mcs.constrainHeight(mv_dash.getId(), 4);
                        mcs.connect(mv_dash.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT);
                        mcs.connect(mv_dash.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT);
                        //底部和父视图对齐
                        mcs.connect(mv_dash.getId(), ConstraintSet.TOP, miv.getId(), ConstraintSet.BOTTOM,1);
                        mv_dash.setBackgroundResource(R.drawable.dash_line);
                        mv_dash.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
//                        mv_dash.setBackgroundColor(Color.parseColor("#ffffffff"));
                    }
                }

                //远程且没通过的
                {
                    if(mtv_checkTime == null) {
                        mtv_checkTime =  new TextView(this.getContext());
                        mtv_checkTime.setId(View.generateViewId());
                        cl_container.addView(mtv_checkTime);
                        mcs.constrainHeight(mtv_checkTime.getId(), ViewGroup.LayoutParams.WRAP_CONTENT);
                        mcs.connect(mtv_checkTime.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT,20);
                        //底部和父视图对齐
                        mcs.connect(mtv_checkTime.getId(), ConstraintSet.TOP, miv.getId(), ConstraintSet.BOTTOM,20);
                        mtv_checkTime.setTextColor(Color.parseColor("#ffffffff"));
                    }
                    String str = String.format("审核日期:%s",model.checkTime);
                    mtv_checkTime.setText(str);

                }
                {
                    //为什么checkReason底部会有多余的距离???
                    if(mtv_checkReason == null) {
                        mtv_checkReason =  new TextView(this.getContext());
                        mtv_checkReason.setId(View.generateViewId());
                        cl_container.addView(mtv_checkReason);
                        mcs.constrainHeight(mtv_checkReason.getId(), ViewGroup.LayoutParams.WRAP_CONTENT);
                        mcs.connect(mtv_checkReason.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT,20);
                        //底部和父视图对齐
                        mcs.connect(mtv_checkReason.getId(), ConstraintSet.TOP, mtv_checkTime.getId(), ConstraintSet.BOTTOM,20);
                        mtv_checkReason.setTextColor(Color.parseColor("#ffffffff"));
//                        mtv_checkReason.setBackgroundColor(Color.parseColor("#ffff0000"));
                    }
                    String str = String.format("审核结果:%s",model.checkResult);
                    mtv_checkReason.setText(str);
                }
            }

            mcs.applyTo(cl_container);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //设置回调

        // 此时为view设置点击函数比较合适,这里提供了position.
        miv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mClickCallback != null) {
                    mClickCallback.onClickItemAtInex(position, model, FilmCell.ActionDelete);
                }
            }
        });


        cl_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickCallback.onClickItemAtInex(position, model, FilmCell.ActionSelect);
            }
        });

        if(!model.isRemote) {
            miv_upload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mClickCallback.onClickItemAtInex(position, model, FilmCell.ActionUpload);
                }
            });
        }
    }
}
